package cn.itsource.pethome.system.controller;

import cn.itsource.pethome.department.domain.Department;
import cn.itsource.pethome.system.domain.Detail;
import cn.itsource.pethome.system.domain.Product;
import cn.itsource.pethome.system.query.DetailQuery;
import cn.itsource.pethome.system.query.ProductQuery;
import cn.itsource.pethome.system.service.IDetailService;
import cn.itsource.pethome.system.service.IProductService;
import cn.itsource.pethome.util.AjaxResult;
import cn.itsource.pethome.util.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/det")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DetailController {
    @Autowired
    IDetailService detailService;

    @PostMapping("/pagelist")
    @ApiOperation(value = "高级查询&分页" )
    public PageList<Detail> pageQueryAll(@RequestBody DetailQuery productQuery){
        System.out.println(productQuery);
        PageList<Detail> productPageList = detailService.pageTypeOne(productQuery);
        System.out.println(productPageList);
        return productPageList;
    }

    @DeleteMapping("/department/{id}")
    @ApiOperation(value = "根据id删除部门" )
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            detailService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @PostMapping("/department")
    @ApiOperation(value = "添加 和更改 部门" )
    public AjaxResult insert(@RequestBody Detail detail){
        System.out.println(detail);
        try {
            if(detail.getId() == null){
                detailService.insert(detail);
            }else {
                detailService.update(detail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

/*    @GetMapping("/list/{id}")
    @ApiOperation(value = "通过ID查询" )
    public List<Detail> queryOne(@PathVariable("id") Long id){
        return detailService.pageTypeOne(id);
    }*/
}
